package MediaOnline.WebService;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import MediaOnline.Data.ThongTinDienVien;
import MediaOnline.GUI.actActorInfo;
import MediaOnline.GUI.actLogin;
import MediaOnline.General.paramConst;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;

public class MediaLogin {
/** Called when the activity is first created. */
    
	private static final String Soap_MethodName = "AndroidCheckLogIn";
	private static final String Soap_Action = paramConst.Webservice_NameSpace+Soap_MethodName;
	private static final String Soap_NameSpace = paramConst.Webservice_NameSpace;
	private static final String Soap_URL = paramConst.Webservice_Url+"FilmUserService.asmx?op="+Soap_MethodName;
	SoapObject request = new SoapObject(Soap_NameSpace, Soap_MethodName);
	public Context mContext;
	public String Username="";
	public String Password="";
	public SoapObject request2;
	public SoapSerializationEnvelope Soap_Evelope;
	ProgressDialog progress; 
	public int status=-1;
	public boolean isOnline=false;
	//ProgressDialog progress; 
		public MediaLogin(Context ctx, String Uname, String Pass){
			mContext = ctx;
			progress = new ProgressDialog(mContext);
			Username = Uname;
			Password = Pass;
		}
		
		public void checkLogin(){
			//Gọi đến asynctask
			checkLogin check = new checkLogin();
			check.execute();
		}

	class checkLogin extends AsyncTask<String, Void, String> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			//Chay progress dialog
			progress.setMessage("Đang đăng nhập với tài khoản "+Username+". Vui lòng chờ");
			progress.setIndeterminate(true);
			progress.setCancelable(false);
			progress.show();
		}


		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			progress.dismiss();
			actLogin.KetQuaDangNhap(status);
		}


		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
	        HttpTransportSE androidHttpTransport = new HttpTransportSE(Soap_URL);
	        request = new SoapObject(Soap_NameSpace, Soap_MethodName);
			Soap_Evelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
	        Soap_Evelope.dotNet=true;
	        //gửi thông số
	        request.addProperty("Account",Username);
			request.addProperty("PassWord",Password);
			Soap_Evelope.setOutputSoapObject(request);
	     	//CallService
	      	try{
	      		for (int i = 0; i < request.getPropertyCount(); i++) {
					Log.d("login property::::::", request.getProperty(i).toString());
				}
	      		androidHttpTransport.call(Soap_Action, Soap_Evelope);
	            //Lay du lieu file xml
	      		SoapObject requestall=(SoapObject) Soap_Evelope.getResponse();
	      		Log.d("Login result::::::::", requestall.toString());
	            //loc cac thong tin thua
	            request = (SoapObject) requestall.getProperty(0);
	            //Lay thong tin lỗi
		        status=Integer.parseInt(request.getAttribute("Error").toString());
			}
			catch(Exception e){
				Log.d("Asynctask exception ::: ", e.toString());

			}
			return null;
		}
		
	}
}
